package  com
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.display.Loader;
	import flash.geom.Point;
	import flash.net.URLRequest;
	/**
	 * ...
	 * @author anzhilin
	 */
	public class Map extends Sprite
	{
		private var _mapRow:uint;
		private var _mapCol:uint;
		private var _mapArr:Array;
		private var mapSpr:Sprite;
		private var _mapWidth:uint;
		private var _mapHeight:uint;
		private var mapXml:XML;
		private var playerList:Array;
		public var _buildingLayer:BuildingLayer;
		public static const TILE_W:uint = 48;
		public static const TILE_H:uint = 24;
		public function Map(xml:XML) 
		{
			mapXml = xml;
			mapSpr = new Sprite();
			addChild(mapSpr);
			_mapRow = mapXml.floor.@row;
			_mapCol = mapXml.floor.@col;
			_mapWidth = mapXml.@mapwidth;
			_mapHeight = mapXml.@mapheight;
			playerList = new Array();
			_buildingLayer = new BuildingLayer(mapXml);
			_mapArr = getArrFromXml(mapXml.floor, _mapRow, _mapCol);
			drawMap();
		}
		private function getArrFromXml(str:String, row:uint, col:uint):Array
		{
			var arr:Array = str.split(',');//将字符串拆分成一维数组
			var index:uint = 0;//一维数组的索引
			var mArr:Array = new Array();// 最终返回的地图数组
			for (var i:uint = 0; i < _mapRow; i++ )
			{
				var tempArr:Array = new Array();//临时数组 相当于地图数组的每一行
				for (var j:uint = 0; j < _mapCol; j++ )
				{
					tempArr.push(int(arr[index]));
					index++;
				}
				mArr.push(tempArr);
			}
			return mArr;
		}
		private function drawMap():void
		{
			/*for (var i:uint = 0; i < _mapRow; i++ )
			{
				for (var j:uint = 0; j < _mapCol; j++ )
				{
					var tile:Tile = new Tile();
					tile.x = j * TILE_W + (i & 1) * TILE_W / 2;
					tile.y = i * TILE_H / 2;
					if (i & 1)
					{
						tile.border.visible = false;
					}
					else
					{
						tile.cross.visible = false;
					}
					mapSpr.addChild(tile);
					if (_mapArr[i][j])
					{
						tile.gotoAndStop("unpassable");
					}
					else
					{
						tile.gotoAndStop("passable");
					}
					
				}
			}
			*/
			var loader:Loader= new Loader();
			loader.load(new URLRequest("./images/map.jpg") );
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onMapLoad);
		}
		private function onMapLoad(evt:Event):void
		{
			var bmd:BitmapData = BitmapData(evt.target.content.bitmapData);
			var bm:Bitmap = new Bitmap(bmd);
			addChild(bm);
			this.addChild(_buildingLayer);
			var grid:GridLayer = new GridLayer();
			grid.drawGrid(_mapRow,_mapCol, TILE_W, TILE_H);
			addChildAt(grid, this.numChildren - 1);
		}
		public function addPlayer(player:Avatar):void
		{
			playerList.push(player);
			_buildingLayer.addChild(player);
		}
		public function controlPlayer(rx:int,ry:int ):void
		{
			trace(playerList[0]);
			var _player:Avatar = playerList[0] as Avatar;
			_player.x = rx+100;
			_player.y = ry+100;
			//var player:Avatar = playerList[0];
			//player.x = rx;
			//player.y = ry;
		}
		/**
		 * 屏幕坐标转世界坐标
		 * @param	mx 屏幕x坐标
		 * @param	my 屏幕y坐标
		 * @return 世界坐标
		 */
		public static function screenToworld(mx:int, my:int):Point
		{
			var rectRow:uint = Math.floor(my / Map.TILE_H);//矩形行
			var rectCol:uint = Math.floor(mx / Map.TILE_W);//矩形列
			var dx:int;
			var dy:int;
			var radian:Number;
			var angle:Number;
			var point:Point;//世界坐标
			if (mx - rectCol*Map.TILE_W <= Map.TILE_W / 2)//左边矩形
			{
				dx = mx - rectCol * Map.TILE_W;
				dy=my-((rectRow+0.5)*Map.TILE_H)
				angle = Math.atan2(dy, dx) * 180 / Math.PI;
				if(angle>=-90&&angle<-30)
				{
					point=new Point(rectRow*2-1,rectCol-1);
				}
				else if (angle > 30 && angle <= 90)
				{
					point=new Point(rectRow*2+1,rectCol-1);
				}
				else
				{
					point=new Point(rectRow*2,rectCol);
				}
			}
			else	//右边矩形
			{
				dx = (rectCol + 1) * Map.TILE_W - mx;
				dy = my - ((rectRow + 0.5) * Map.TILE_H);
				angle = Math.atan2(dy, dx) * 180 / Math.PI;
				if (angle >= -90 && angle < -30)
				{
					point=new Point(rectRow*2-1,rectCol);
				}
				else if (angle >= 30 && angle <=90)
				{
					point=new Point(rectRow*2+1,rectCol);
				}
				else
				{
					point=new Point(rectRow*2,rectCol);
				}
			}
			return point;
		}
		public function get mapWidth():uint
		{
			return _mapWidth;
		}
		public function get mapHeight():uint
		{
			return _mapHeight;
		}
		public function get mapRow():uint
		{
			return _mapRow;
		}
		public function get mapCol():uint
		{
			return _mapCol;
		}
		public function get mapArr():Array
		{
			return _mapArr;
		}
		
	}

}